<?php
#############################################################################
# Set of classes wrapping channels list entities.                           #
# Also contains parser used to convert raw xml list into wrapping classes.  #
# Used to simplify further list handling.                                   #
#                                                                           #
# Author: consros 2010                                                      #
#############################################################################

require_once("settings.inc");
require_once("lang.inc");
require_once("rtvFunctions.inc");

class Program {
    var $name;
    var $details;
    var $beginTime;

    function Program($name, $details, $beginTime) {
        $this->name = $name;
        $this->details = $details;
        $this->beginTime = $beginTime;
    }
}

class Channel {
    var $id;
    var $alias;
    var $name;
    var $logo;
    var $format;
    var $lang;
    var $categoryId;

    var $program;
    var $beginTime;
    var $endTime;

    function Channel($id, $alias, $name, $logo, $format, $lang, $categoryId) {
        $this->id         = $id;
        $this->alias      = $alias;
        $this->name       = $name;
        $this->logo       = $logo;
        $this->format     = $format;
        $this->lang       = $lang;
        $this->categoryId = $categoryId;
    }
}

class Category {
    var $id;
    var $name;
    var $channels;

    function Category($id, $name) {
        $this->id       = $id;
        $this->name     = $name;
        $this->channels = array();
    }

    function addChannel($channel) {
        $this->channels[] = $channel;
    }

    function updateName() {
        if (count($this->channels) > 0) {
            $this->name .= " (" . count($this->channels) . ")";
        }
    }
}

class ChannelsParser {
    var $selectedChannel;
    var $channelsAmount;
    var $categories;

    function ChannelsParser() {
        $this->selectedChannel = 0;
        $this->channelsAmount = 0;
        $this->categories = array();
    }

    function parse($params) {
        $this->categories = array();
        $counter = 0;
        foreach (explode(',', LANG_AVAIL_CATEGORIES) as $cat) {
            $this->categories[] = new Category($counter++, $cat);
        }
        foreach ($params['tv'] as $info) {
            $channel = new Channel($info['id'], $info['alias'], 
                $info['name'], "http://" . BASE_URL . "/" . $info['ico'], 
                $info['ar'], $info['lng'], $info['genre_id']);
            $i = $channel->categoryId;
            if (! isset($i) || $i < 0 || $i >= count($this->categories)) {
                $i = 0;
            }
            $this->categories[$i]->addChannel($channel);
            $this->channelsAmount++;
        }
        foreach ($this->categories as $category) {
            $category->updateName();
        }        
    }
}

class ProgramsParser {
    var $programs;

    function ProgramsParser() {
        $this->programs = array();
    }

    function parse($params) {
        $this->programs = array();
        foreach ($params['epg'] as $info) {
            $this->programs[] = new Program(
                $info['name'], $info['descr'], $info['time']);
        }
    }
}
?>
